package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Account;
import org.csu.mypetstore.api.entity.Profile;
import org.csu.mypetstore.api.entity.Signon;
import org.csu.mypetstore.api.persistence.AccountMapper;
import org.csu.mypetstore.api.persistence.BannerDataMapper;
import org.csu.mypetstore.api.persistence.ProfileMapper;
import org.csu.mypetstore.api.persistence.SignonMapper;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.service.RedisService;
import org.csu.mypetstore.api.util.MD5Utils;
import org.csu.mypetstore.api.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    public AccountMapper accountMapper;
    @Autowired
    public SignonMapper signonMapper;
    @Autowired
    public ProfileMapper profileMapper;
    @Autowired
    public BannerDataMapper bannerDataMapper;
    @Autowired
    private RedisService redisService;

    //登录
    @Override
    public CommonResponse<AccountVO> login(Signon signon) {
        int rows = signonMapper.selectCount(Wrappers.<Signon>query().eq("username",signon.getUsername()));
       if (rows ==0){
           return CommonResponse.createForError("用户名不存在");
       }
       String password = MD5Utils.md5(signon.getPassword());
       Signon signon1 = signonMapper.selectOne(Wrappers.<Signon>query().eq("username",signon.getUsername()).eq("password",password));
        if (signon1 == null){
            return CommonResponse.createForError("用户名或密码错误");
        }

        Account account = accountMapper.selectById(signon1.getUsername());
        AccountVO accountVO = accountToAccountVO(account);
        String token = UUID.randomUUID().toString();
        redisService.set(token, signon1.getUsername());
        return CommonResponse.createForLogin("登录成功",token,accountVO);
    }
    //查询指定用户信息
    @Override
    public CommonResponse<AccountVO> getAccountByUserId(String userId){
        Account account = accountMapper.selectById(userId);
        if (account==null){
            return CommonResponse.createForError("无此用户");
        }
        AccountVO accountVO = accountToAccountVO(account);
        return CommonResponse.createForSuccess(accountVO);
    }
    //查询所有用户信息
    @Override
    public CommonResponse<List<AccountVO>> getAccountList(){
        List<Account> accountList = accountMapper.selectList(null);

        List<AccountVO> accountVOList = new ArrayList<>();
        for (Account account :accountList){
            AccountVO accountVO = accountToAccountVO(account);
            accountVOList.add(accountVO);
        }

        if (accountList.isEmpty()){
            return CommonResponse.createForSuccessMessage("无用户数据");
        }
        return CommonResponse.createForSuccess(accountVOList);
    }
    //查询profile
    @Override
   public CommonResponse<List<Profile>> getallAccountprofile(){
        List<Profile> profileList = profileMapper.selectList(null);
        if(profileList.isEmpty()){
            return CommonResponse.createForSuccessMessage("无数据");
        }
        return CommonResponse.createForSuccess(profileList);
    }
    //查询密码
    public AccountVO accountToAccountVO(Account account){
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUsername());
        accountVO.setEmail(account.getEmail());
        accountVO.setStatus(account.getStatus());
        accountVO.setState(account.getState());
        accountVO.setLastName(account.getLastName());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setCity(account.getCity());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setZip(account.getZip());
        accountVO.setPhone(account.getPhone());
        accountVO.setCountry(account.getCountry());

        Signon signon = signonMapper.selectById(account.getUsername());

        Profile profile = profileMapper.selectById(account.getUsername());

       // BannerData bannerData = bannerDataMapper.selectById()

        accountVO.setPassword(signon.getPassword());
        accountVO.setLanguagePreference(profile.getLanguagePreference());
        accountVO.setFavouriteCategoryId(profile.getFavouriteCategoryId());

        return accountVO;
    }
    //添加用户
    @Override
    public CommonResponse<String> insertAccount(AccountVO accountVO){
       int rows = accountMapper.selectCount(Wrappers.<Account>query().eq("userid",accountVO.getUsername()));
       if (rows>0){
           return CommonResponse.createForError("用户名已存在");
       }

        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setStatus(accountVO.getStatus());
        account.setState(accountVO.getState());
        account.setPhone(accountVO.getPhone());
        account.setLastName(accountVO.getLastName());
        account.setFirstName(accountVO.getFirstName());
        account.setEmail(accountVO.getEmail());
        account.setCountry(accountVO.getCountry());
        account.setCity(accountVO.getCity());
        account.setAddress2(accountVO.getAddress2());
        account.setAddress1(accountVO.getAddress1());
        account.setZip(accountVO.getZip());

        Signon signon = new Signon();
        signon.setUsername(accountVO.getUsername());
        String password = accountVO.getPassword();
        // 将密码进行MD5加密
        password= MD5Utils.md5(password);
        signon.setPassword(password);

        Profile profile = new Profile();
        profile.setUsername(accountVO.getUsername());
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        profile.setLanguagePreference(accountVO.getLanguagePreference());

        // BannerData bannerData = bannerDataMapper.selectById()



        rows = accountMapper.insert(account);
        int rows1 = signonMapper.insert(signon);
        int rows2 = profileMapper.insert(profile);

       if (rows == 0||rows1 == 0||rows2 == 0){
           return CommonResponse.createForError("注册失败");
       }
        return  CommonResponse.createForSuccess("添加成功");
    }

    @Override
    public  CommonResponse<String> deleteAccount(Account account){

        return  CommonResponse.createForSuccess("删除成功");
    }

    @Override
    public  CommonResponse<String> updateAccount(AccountVO accountVO){
        int rows = accountMapper.selectCount(Wrappers.<Account>query().eq("userid",accountVO.getUsername()));
        if (rows==0){
            return CommonResponse.createForError("用户名不存在");
        }

        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setStatus(accountVO.getStatus());
        account.setState(accountVO.getState());
        String phone = accountVO.getPhone();
        Account account1 = accountMapper.selectOne(Wrappers.<Account>query().eq("phone",phone));
        if (account1!=null){
            return CommonResponse.createForError("此电话号码已被注册");
        }
        account.setPhone(accountVO.getPhone());
        account.setLastName(accountVO.getLastName());
        account.setFirstName(accountVO.getFirstName());
        account.setEmail(accountVO.getEmail());
        account.setCountry(accountVO.getCountry());
        account.setCity(accountVO.getCity());
        account.setAddress2(accountVO.getAddress2());
        account.setAddress1(accountVO.getAddress1());
        account.setZip(accountVO.getZip());

        Signon signon = new Signon();
        signon.setUsername(accountVO.getUsername());
        String password = accountVO.getPassword();
        Signon signon1 = signonMapper.selectOne(Wrappers.<Signon>query().eq("userid",accountVO.getUsername()));
        if (password == signon1.getPassword()) {
            signon.setPassword(password);
        }else {
            // 将密码进行MD5加密
            password= MD5Utils.md5(password);
            signon.setPassword(password);
        }

        Profile profile = new Profile();
        profile.setUsername(accountVO.getUsername());
        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        profile.setLanguagePreference(accountVO.getLanguagePreference());

        // BannerData bannerData = bannerDataMapper.selectById()

        rows = accountMapper.update(account,Wrappers.<Account>query().eq("userid",account.getUsername()));
        int rows1 = signonMapper.update(signon,Wrappers.<Signon>query().eq("username",signon.getUsername()));
        int rows2 = profileMapper.update(profile,Wrappers.<Profile>query().eq("userid",profile.getUsername()));

        if (rows == 0||rows1 == 0||rows2 == 0){
            return CommonResponse.createForError("更新失败");
        }

        return  CommonResponse.createForSuccess("更新成功");
    }

    @Override
    public CommonResponse<AccountVO> getAccountByPhone(String phone) {
        Account account = accountMapper.selectOne(Wrappers.<Account>query().eq("phone",phone));
        if (account==null){
            return CommonResponse.createForError("无此用户");
        }
        AccountVO accountVO = accountToAccountVO(account);
        return CommonResponse.createForSuccess(accountVO);
    }
}
